package com.tnovoselec.android.travelassistant.parser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import com.tnovoselec.android.travelassistant.dto.ForecastFavoritesDto;
import com.tnovoselec.android.travelassistant.dto.MeteoFavoritesDto;



public class MeteoParser extends DefaultHandler {

	private StringBuilder buffer;
	// private WeatherDto weather;
	// private Forecast tempForecast;

	private ForecastFavoritesDto tempForecast;

	private final static String TAG_WEATHER = "weathers";

	private final static String TAG_CURRENT_CONDITIONS = "current_condition";
	private final static String TAG_CONDITION = "weatherDesc";
	private final static String TAG_TEMP_C = "temp_C";
	private final static String TAG_HUMIDITY = "humidity";
	private final static String TAG_ICON = "icon";
	private final static String TAG_WIND_DIRECTION = "wind_direction";
	private final static String TAG_WIND_SPEED = "wind_speed";
	private final static String TAG_BACKGROUND = "background";
	private final static String TAG_FORECAST_CONDITIONS = "weather";
	private final static String TAG_DAY_OF_WEEK = "day_of_week";
	private final static String TAG_LOW = "tempMinC";
	private final static String TAG_HIGH = "tempMaxC";
	private final static String TAG_WEATHER_CODE = "weatherCode";

	
	private List<MeteoFavoritesDto> weathers;
	private MeteoFavoritesDto tempWeather;

	private final static String TAG_ITEM = "item";
	private final static String TAG_TITLE = "title";
	private final static String TAG_URL = "url";
	private final static String TAG_ID = "id";
	private final static String TAG_LAT = "lat";
	private final static String TAG_LNG = "lng";

	private boolean inItem = false;
	private boolean inTitle = false;
	private boolean inUrl = false;
	private boolean inId = false;
	private boolean inLat= false;
	private boolean inLng = false;
//	private boolean inWeather = false;

	private int position = 0;

	private final static String ATTR_DATA = "data";
	private final static String ATTR_UNIT = "unit";

	private boolean inCurrentConditions = false;
	private boolean inForecastConditions = false;

	@Override
	public void startDocument() throws SAXException {
		weathers = new ArrayList<MeteoFavoritesDto>();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		buffer = new StringBuilder("");

		if (localName.equalsIgnoreCase(TAG_ITEM)) {
			inItem = true;
			tempWeather = new MeteoFavoritesDto();
		} else if (localName.equalsIgnoreCase(TAG_TITLE)) {
			inTitle = true;
		} else if (localName.equalsIgnoreCase(TAG_URL)) {
			inUrl = true;
		} else if (localName.equalsIgnoreCase(TAG_ID)) {
			inId = true;
		} else if (localName.equalsIgnoreCase(TAG_LAT)) {
			inLat = true;
		} else if (localName.equalsIgnoreCase(TAG_LNG)) {
			inLng = true;
		} else if (localName.equalsIgnoreCase(TAG_WEATHER)) {
		//	inWeather = true;
		} else if (localName.equalsIgnoreCase(TAG_CURRENT_CONDITIONS)) {
			inCurrentConditions = true;
		} else if (localName.equalsIgnoreCase(TAG_CONDITION)) {
			
			if (inCurrentConditions) {
				tempWeather.setConditionString(attributes.getValue(ATTR_DATA));
			} else if (inForecastConditions) {
				tempForecast.setConditionString(attributes.getValue(ATTR_DATA));
			}
		} else if (localName.equalsIgnoreCase(TAG_TEMP_C)) {
			try {
				tempWeather.setTemperature(Integer.parseInt(attributes.getValue(ATTR_DATA)));
			} catch (Exception e) {
				tempWeather.setTemperature(0);
			}
		} else if (localName.equalsIgnoreCase(TAG_HUMIDITY)) {
			tempWeather.setHumidity(attributes.getValue(ATTR_DATA));
			//tempWeather.setHumidity(humidity)
		} else if (localName.equalsIgnoreCase(TAG_ICON)) {
			if (inCurrentConditions) {
				tempWeather.setIconUrl(attributes.getValue(ATTR_DATA));
			} else if (inForecastConditions) {
				tempForecast.setIconUrl(attributes.getValue(ATTR_DATA));
			}
		} else if (localName.equalsIgnoreCase(TAG_WIND_DIRECTION)) {
			tempWeather.setWindDirection(attributes.getValue(ATTR_DATA));
		} else if (localName.equalsIgnoreCase(TAG_BACKGROUND)) {
			tempWeather.setBackgroundImageUrl(attributes.getValue(ATTR_DATA));
		} else if (localName.equalsIgnoreCase(TAG_WIND_SPEED)) {
			try {
				tempWeather.setWindSpeed(Integer.parseInt(attributes.getValue(ATTR_DATA)));
			} catch (Exception e) {
				tempWeather.setWindSpeed(0);
			}
			tempWeather.setWindSpeedUnit(attributes.getValue(ATTR_UNIT));
		} else if (localName.equalsIgnoreCase(TAG_FORECAST_CONDITIONS)) {
			inForecastConditions = true;
			tempForecast = new ForecastFavoritesDto();
		} else if (localName.equalsIgnoreCase(TAG_DAY_OF_WEEK)) {
			tempForecast.setDayOfWeekString(attributes.getValue(ATTR_DATA));
		} else if (localName.equalsIgnoreCase(TAG_LOW)) {
			try {
				tempForecast.setTemperatureLow(Integer.parseInt(attributes.getValue(ATTR_DATA)));
			} catch (Exception e) {
				tempForecast.setTemperatureLow(0);
			}
		} else if (localName.equalsIgnoreCase(TAG_HIGH)) {
			try {
				tempForecast.setTemperatureHigh(Integer.parseInt(attributes.getValue(ATTR_DATA)));
			} catch (Exception e) {
				tempForecast.setTemperatureHigh(0);
			}
		} else if (localName.equalsIgnoreCase(TAG_WEATHER_CODE)) {
			if (inCurrentConditions) {
				tempWeather.setWeatherCode(Integer.parseInt(attributes.getValue(ATTR_DATA)));
			} else if (inForecastConditions) {
				tempForecast.setWeatherCode(Integer.parseInt(attributes.getValue(ATTR_DATA)));
			}
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {

		if (inItem) {
			if (inTitle) {
				tempWeather.setTitle(buffer.toString());
			} else if (inId) {
				tempWeather.setId(Integer.valueOf(buffer.toString()));
			} else if (inLat) {
				tempWeather.setLat(Double.valueOf(buffer.toString()).doubleValue());
			} else if (inLng) {
				tempWeather.setLng(Double.valueOf(buffer.toString()).doubleValue());
			} else if (inUrl) {
				tempWeather.setUrl(buffer.toString());
			}
		}

		if (localName.equalsIgnoreCase(TAG_CURRENT_CONDITIONS)) {
			inCurrentConditions = false;
		} else if (localName.equalsIgnoreCase(TAG_FORECAST_CONDITIONS)) {
			inForecastConditions = false;
			tempForecast.setCityId(tempWeather.getCityId());
			tempWeather.addForecast(tempForecast);
		} else if (localName.equalsIgnoreCase(TAG_ITEM)) {
			inItem = false;
			tempWeather.setPosition(position++);
			weathers.add(tempWeather);
		} else if (localName.equalsIgnoreCase(TAG_TITLE)) {
			inTitle = false;
		} else if (localName.equalsIgnoreCase(TAG_URL)) {
			inUrl = false;
		} else if (localName.equalsIgnoreCase(TAG_ID)) {
			inId = false;
		}else if (localName.equalsIgnoreCase(TAG_LAT)) {
			inLat = false;
		}else if (localName.equalsIgnoreCase(TAG_LNG)) {
			inLng = false;
		}else if (localName.equalsIgnoreCase(TAG_WEATHER)) {
			//inWeather = false;
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		buffer.append(String.valueOf(ch, start, length));
	}

	/**
	 * Method used to get weather data from one location
	 * 
	 * @return weather data for specific location
	 */
	public List<MeteoFavoritesDto> getWeathers() {
		return weathers;
	}

}
